import * as api from "@/api";

export default {
  namespaced: true,

  state: {
    cards: [],
  },

  getters: {
    getterCards: ({ cards }) => (boardListId) =>
      cards.filter((card) => card.boardListId == boardListId),

    getterCard: ({ cards }) => (cardId) =>
      cards.find((card) => card.id == cardId),
  },

  mutations: {
    updateCards: (state, datas) => {
      state.cards = [...state.cards, ...datas];
    },

    editCard: (state, data) => {
      state.cards = state.cards.map((card) => {
        if (card.id === data.id) {
          return data;
        }

        return card;
      });
    },

    addAttachment: (state, data) => {
      state.cards = state.cards.map((card) => {
        if (card.id == data.boardListCardId) {
          return {
            ...card,
            attachments: [...card.attachments, data],
          };
        }
        return card;
      });
    },

    removeAttachment: (state, data) => {
      state.cards = state.cards.map((card) => {
        if (card.id == data.cardId) {
          return {
            ...card,
            attachments: card.attachments.filter((item) => item.id !== data.id),
          };
        }
        return card;
      });
    },

    setCover: (state, data) => {
      state.cards = state.cards.map((card) => {
        if (card.id == data.cardId) {
          return {
            ...card,
            attachments: card.attachments.map((attachment) => {
              return {
                ...attachment,
                isCover: attachment.id === data.id,
              };
            }),
          };
        }
        return card;
      });
    },

    removeCover: (state, data) => {
      state.cards = state.cards.map((card) => {
        if (card.id == data.cardId) {
          return {
            ...card,
            attachments: card.attachments.map((attachment) => {
              return {
                ...attachment,
                isCover: false,
              };
            }),
          };
        }
        return card;
      });
    },
  },

  actions: {
    getCards: async ({ commit }, boardListId) => {
      let rs = await api.getCards(boardListId);

      commit("updateCards", rs.data);

      return rs;
    },

    postCard: async ({ commit }, body) => {
      await api.postCard(body);

      let rs = await api.getCards(body.boardListId);

      commit("updateCards", rs.data);
    },

    putCard: async ({ commit }, body) => {
      let { data } = await api.putCard(body);

      commit("editCard", data);
    },

    uploadAttachment: async ({ commit }, body) => {
      try {
        let { data } = await api.uploadAttachment(body);

        commit("addAttachment", data);
      } catch (error) {
        console.log(error);
      }
    },

    setCover: async ({ commit }, body) => {
      try {
        await api.setCover({ id: body.id });

        commit("setCover", body);
      } catch (error) {
        console.log(error);
      }
    },

    removeCover: async ({ commit }, body) => {
      try {
        await api.removeCover({ id: body.id });

        commit("removeCover", body);
      } catch (error) {
        console.log(error);
      }
    },

    removeAttachment: async ({ commit }, body) => {
      try {
        await api.removeAttachment({ id: body.id });

        commit("removeAttachment", body);
      } catch (error) {
        console.log(error);
      }
    },
  },
};
